package com.cskaoyan.controller.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.mallbean.Region;
import com.cskaoyan.bean.userbean.Address;
import com.cskaoyan.service.wx.WxAddressService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.wx.WxAddressDetailVo;
import com.cskaoyan.vo.wx.WxAddressListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    public Integer getId(){
        return MyUtils.getUserId();
//        return 1;
    }




    /**
     * 获取当前用户的地址信息
     * @return
     */
    @RequestMapping("address/list")
    public BaseRespVo list(){
        int userId = getId();
        List<WxAddressListVo> wxAddressListVoList = wxAddressService.list(userId);
        return BaseRespVo.ok(wxAddressListVoList);

    }

    /**
     * 根据id获取地址详情
     * @param id
     * @return
     */
    @RequestMapping("address/detail")
    public BaseRespVo detail(Integer id) {
        WxAddressDetailVo wxAddressDetailVo = wxAddressService.detail(id);
        return BaseRespVo.ok(wxAddressDetailVo);

    }

    /**
     * 根据pid返回省市县列表
     * @param pid
     * @return
     */
    @RequestMapping("region/list")
    public BaseRespVo listOfRegion(Integer pid){
        List<Region> regionList = wxAddressService.listOfRegion(pid);
        return BaseRespVo.ok(regionList);
    }

    /**
     * 修改或新增地址
     * @param address
     * @return
     */
    @PostMapping("address/save")
    public BaseRespVo save(@RequestBody Address address){
        address.setUserId(getId());
        address.setDeleted(false);
        int id = wxAddressService.save(address);
        return BaseRespVo.ok(id);
    }

    /**
     * 根据地址表id删除地址（逻辑删除）
     * @param
     * @return
     */
    @PostMapping("address/delete")
    public BaseRespVo delete(@RequestBody Map map) {
        wxAddressService.delete((Integer) map.get("id"));
        return BaseRespVo.ok();
    }



}
